#!/bin/bash
#存储节点安装配置cinder-volume服务
#http://www.cnblogs.com/elvi/p/7736746.html
#cinder后端采用lvm、nfs安装配置

#首先为lvm、nfs创建各自的分区

nodename=`hostname`
read -p "input ethernet name:" netname
if [ -z $netname ]
then
  netname=eth1
fi

nodeip=`ifconfig $netname | grep 'inet' | grep -v 'inet6' | cut -d: -f2 | awk '{ print $2}' `

#安装配置Cinder
yum install -y openstack-cinder targetcli python-keystone lvm2
systemctl enable lvm2-lvmetad.service
systemctl start lvm2-lvmetad.service

##############
#安装配置NFS服务，作为后端存储使用
yum install nfs-utils rpcbind -y
mkdir -p /cinder/nfs/{cinder_nfs1,cinder_nfs2}
chown cinder:cinder /cinder/nfs/cinder_nfs1
chmod 777 /cinder/nfs/cinder_nfs1
#echo "/date/cinder_nfs1 *(rw,no_root_squash,sync)">/etc/exports
echo "/cinder/nfs/cinder_nfs1 *(rw,root_squash,sync,anonuid=165,anongid=165)">/etc/exports
exportfs -r
systemctl enable rpcbind nfs-server
systemctl restart rpcbind nfs-server
showmount -e localhost

######################################################


cp /etc/cinder/cinder.conf{,.bak}
cp /etc/lvm/lvm.conf{,.bak}

#配置LVM过滤，只接收上面配置的lvm设备/dev/sdb2
#在devices {  }部分添加 filter = [ "a/sdb2/", "r/.*/"]
sed -i '141a filter = [ "a/sda5/", "r/.*/"]' /etc/lvm/lvm.conf  #在141行后添加

#NFS
echo $nodeip':/cinder/nfs/cinder_nfs1'>/etc/cinder/nfs_shares
chmod 640 /etc/cinder/nfs_shares
chown root:cinder /etc/cinder/nfs_shares

#Cinder配置
echo '
#cinder volume
[DEFAULT]
auth_strategy = keystone
log_dir = /var/log/cinder
state_path = /var/lib/cinder
glance_api_servers = http://controller:9292
transport_url = rabbit://openstack:openstack@controller:5673
enabled_backends = lvm,nfs

[database]
connection = mysql+pymysql://cinder:cinder@controller/cinder

[keystone_authtoken]
auth_uri = http://controller:5000
auth_url = http://controller:35357
memcached_servers = controller1:11211,controller2:11211,controller3:11211
auth_type = password
project_domain_name = default
user_domain_name = default
project_name = service
username = cinder
password = cinder

[oslo_concurrency]
lock_path = /var/lib/cinder/tmp

[lvm]
volume_driver = cinder.volume.drivers.lvm.LVMVolumeDriver
iscsi_helper = lioadm
iscsi_protocol = iscsi
volume_group = cinder_lvm01
iscsi_ip_address = '$nodeip'
volumes_dir = $state_path/volumes
volume_backend_name = lvm01

[nfs]
volume_driver = cinder.volume.drivers.nfs.NfsDriver
nfs_shares_config = /etc/cinder/nfs_shares
nfs_mount_point_base = $state_path/mnt
volume_backend_name = nfs01
'>/etc/cinder/cinder.conf
chmod 640 /etc/cinder/cinder.conf 
chgrp cinder /etc/cinder/cinder.conf 

#启动Cinder卷服务
systemctl enable openstack-cinder-volume.service target.service
systemctl start openstack-cinder-volume.service target.service